package banner

import (
	"gorm.io/gorm"
	"time"
)

type Banner struct {
	Id        int32     `json:"id" gorm:"column:id"`
	Title     string    `json:"title" gorm:"column:title"`
	ImageUrl  string    `json:"image_url" gorm:"column:image_url"`
	LinkType  int8      `json:"link_type" gorm:"column:link_type"`
	Link      string    `json:"link" gorm:"column:link"`
	Status    int8      `json:"status" gorm:"column:status"`
	Sort      int8      `json:"sort" gorm:"column:sort"`
	CreatedAt string    `json:"created_at" gorm:"column:created_at"`
	UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at"`
	DeletedAt time.Time `json:"deleted_at" gorm:"column:deleted_at"`
}

func (*Banner) TableName() string {
	return "hx_banners"
}

func NewBanner() *Banner {
	return &Banner{}
}

//获取所有的banners
func (b *Banner) Get(db *gorm.DB) ([]*Banner, error) {
	banners := make([]*Banner, 0)
	err := db.Model(&Banner{}).Where("status = 1 and deleted_at is null").Order("id desc").Find(&banners).Error
	if err != nil {
		return nil, err
	}
	for k, v := range banners {
		banners[k].CreatedAt = v.UpdatedAt.Format("2006-01-02 15:04:05")
	}
	return banners, nil
}
